package com.mtech.aircdx;

import com.mtech.modbus_rtu.codec.impl.ModbusRTUCodec;
import com.mtech.modbus_rtu.data._06.OneWriteRxDataGram;
import com.mtech.modbus_rtu.data._06.OneWriteTxDataGram;
import com.mtech.modbus_rtu.util.ByteUtils;

/**
 * 设置空调状态(寄存器)
 * 
 * @author lixiaolong
 * @CreateDate 2018年3月20日
 */
public class AirCdxWriter {
	ModbusRTUCodec codec;
	byte addr;

	private AirCdxWriter(int addr) {
		this.addr = ByteUtils.int2Bytes(addr, 1)[0];
		this.codec = ModbusRTUCodec.getInstance();
	}

	/**
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param addr
	 *            设备地址
	 * @return
	 */
	public static AirCdxWriter getInstance(int addr) {
		return new AirCdxWriter(addr);
	}

	///////////

	/**
	 * 设置电源状态
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0.停機 1.運轉
	 * @return
	 */
	public byte[] writePowerState(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.POWER_STATE, value);
		return codec.encode(tx);
	}

	/**
	 * 设置锁定状态
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0.解鎖 1.鎖定
	 * @return
	 */
	public byte[] writeLockState(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.LOCK_STATE, value);
		return codec.encode(tx);
	}

	/**
	 * 设置模式
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            寫入範圍：0~3 (4~6 為自動模式反饋狀態) 0.自動空調 1.制冷 2.送風 3.制暖 4.自動制冷 5.自動送風
	 *            6.自動制暖
	 * @return
	 */
	public byte[] writeModeSetting(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.MODE_SETTING, value);
		return codec.encode(tx);
	}

	/**
	 * 風速設定
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0.自動 1.（強速）2.（中速）3.（弱速）
	 * @return
	 */
	public byte[] writeWindSpeedSetting(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.WIND_SPEED_SETTING, value);
		return codec.encode(tx);
	}

	/**
	 * 温度设定
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0~70 =>0~35.0'C
	 * @return
	 */
	public byte[] writeTemplateSetting(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.TEMPLATE_SETTING, value);
		return codec.encode(tx);
	}

	/**
	 * 室溫補償
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0~32 => -8.0~+8.0'C
	 * @return
	 */
	public byte[] writeIndoorTemplateCompensation(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.INDOOR_TEMPLATE_COMPENSATION, value);
		return codec.encode(tx);
	}

	/**
	 * 制暖設溫上限
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0~70 =>0~35.0'C
	 * @return
	 */
	public byte[] writeWarmUpper(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.WARM_UPPER, value);
		return codec.encode(tx);
	}

	/**
	 * 制冷設溫下限
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0~70 =>0~35.0'C
	 * @return
	 */
	public byte[] writeCoolLowwer(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.COOL_LOWWER, value);
		return codec.encode(tx);
	}

	/**
	 * 阀全行程
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0~255 秒(全開) (停機才能設定)
	 * @return
	 */
	public byte[] writeValveFullyDistance(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.VALVE_FULLY_DISTANCE, value);
		return codec.encode(tx);
	}

	/**
	 * 阀门最大允许开度
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            范围 3~10 数值 X10%，3=30%；10=100%
	 * @return
	 */
	public byte[] writeValveOpenMax(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.VALVE_OPEN_MAX, value);
		return codec.encode(tx);
	}

	/**
	 * 四管式冷暖
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param value
	 *            0.二管式 1.四管式 (停機才能設定)
	 * @return
	 */
	public byte[] writeFourTubes(int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, AirCdxRegAddr.FOUR_TUBES, value);
		return codec.encode(tx);
	}

	////////

	/**
	 * 设置寄存器状态
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param registerAddr
	 *            起始寄存器地址
	 * @param value
	 *            需要设置的值
	 * @return
	 */
	public byte[] writeRegister(byte registerAddr, int value) {
		OneWriteTxDataGram tx = new OneWriteTxDataGram(this.addr, registerAddr, value);
		return codec.encode(tx);
	}

	/**
	 * 获取设备写入响应
	 * 
	 * @Author lixiaolong
	 * @CreateDate
	 * @param buf
	 *            设备回传的字节流
	 * @return
	 */
	public OneWriteRxDataGram readRegister(byte[] buf) {
		OneWriteRxDataGram rx = new OneWriteRxDataGram(buf);
		return rx;
	}

}
